﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.DAL;
using QLHD.BLL;

namespace QLHD
{
    public partial class frmDonVi : Form
    {
        public frmDonVi() { }
        public frmDonVi(frmMain form)
        {
            InitializeComponent();
            MdiParent = form;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Thêm")
            {
                btnThem.Text = "Lưu";
                NotReadOnlyTextbox();
                ClearTextbox();
            }
            else
            {
                InsertDV();
                frmDonVi_Load(sender, e);
                btnThem.Text = "Thêm";
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DonVi DV = new DonVi();
            DV.MaDV = txtMaDV.Text;

            DonVi_DB.DeleteDV(DV);
            frmDonVi_Load(sender, e);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "Sửa")
            {
                btnSua.Text = "Lưu";
                NotReadOnlyTextbox();
                txtMaDV.ReadOnly = true;
            }
            else
            {
                UpdateDV();
                frmDonVi_Load(sender, e);
                btnSua.Text = "Sửa";

            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmDonVi_Load(sender, e);
            btnThem.Text = "Thêm";
            btnSua.Text = "Sửa";
        }

        private void frmDonVi_Load(object sender, EventArgs e)
        {
            ReadOnlyTextbox();
            dgvDonVi.DataSource = DonVi_DB.SelectDV();
            txtMaDV.Text = dgvDonVi[0, 0].Value.ToString();
            txtTenDV.Text = dgvDonVi[1, 0].Value.ToString();
        }
        void ReadOnlyTextbox()
        {
            txtMaDV.ReadOnly = true;
            txtTenDV.ReadOnly = true;

        }
        void NotReadOnlyTextbox()
        {
            txtMaDV.ReadOnly = false;
            txtTenDV.ReadOnly = false;

        }
        void InsertDV()
        {
            try
            {                
                DonVi DV = new DonVi(txtMaDV.Text,txtTenDV.Text);

                DonVi_DB.InsertDV(DV);
            }
            catch (SqlException ex)
            {
                string message = "Xảy ra lỗi:" + "\n" + ex.Message;
                MessageBox.Show(message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }
        void ClearTextbox()
        {
            txtMaDV.Clear();
            txtTenDV.Clear();
            ;
        }
        void UpdateDV()
        {
            try
            {
                DonVi DV = new DonVi();
                DV.MaDV = txtMaDV.Text;
                DV.TenDV = txtTenDV.Text;

                DonVi_DB.UpdateDV(DV);
            }
            catch (SqlException ex)
            {
                string message = "Xảy ra lỗi:" + "\n" + ex.Message;
                MessageBox.Show(message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dgvDonVi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaDV.Text = dgvDonVi.Rows[e.RowIndex].Cells[0].Value.ToString();
            txtTenDV.Text = dgvDonVi.Rows[e.RowIndex].Cells[1].Value.ToString();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
